package com.ruoyi.project.module.lys;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.ICxcyAchievementService;
import com.ruoyi.project.service.ICxcyBaseinfoService;
import com.ruoyi.project.service.ICxcyExpenseService;
import com.ruoyi.project.service.ISysUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 费用信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/lysexpense")
public class LysCxcyExpenseController extends BaseController {
    @Autowired
    private ICxcyExpenseService cxcyExpenseService;
    @Autowired
    private ICxcyBaseinfoService cxcyBaseinfoService;
    @Autowired
    private ICxcyAchievementService cxcyAchievementService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询费用信息列表
     */
    @RequiresPermissions("project:lysexpense:list")
    @GetMapping("/list")
    public TableDataInfo list(CxcyExpense cxcyExpense) {
        startPage();
        List<CxcyExpense> list = cxcyExpenseService.selectCxcyExpenseList(cxcyExpense);
        return getDataTable(list);
    }

    /**
     * 导出费用信息列表
     */
    @RequiresPermissions("project:lysexpense:export")
    @Log(title = "费用信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyExpense cxcyExpense, CxcyBaseinfo cxcyBaseinfo) {


        List<CxcyExpense> list = cxcyExpenseService.selectCxcyExpenseList(cxcyExpense);
        ExcelUtil<CxcyExpense> util = new ExcelUtil<CxcyExpense>(CxcyExpense.class);


        util.exportExcel(response, list, "费用信息数据");
    }

    /**
     * 获取费用信息详细信息
     */
    @RequiresPermissions("project:lysexpense:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,CxcyExpense cxcyExpense) {




        return success(cxcyExpenseService.selectCxcyExpenseById(id));
    }

    /**
     * 新增费用信息
     */
    @RequiresPermissions("project:lysexpense:add")
    @Log(title = "费用信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyExpense cxcyExpense) {
        return toAjax(cxcyExpenseService.insertCxcyExpense(cxcyExpense));
    }

    /**
     * 修改费用信息
     */
    @RequiresPermissions("project:lysexpense:edit")
    @Log(title = "费用信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyExpense cxcyExpense) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyExpense.setApplyName(sysUser.getNickName());
        System.out.println("haode");
        cxcyExpense.setApplyNumber(sysUser.getUserName());
        cxcyExpense.setApplyTime(new Date());
        cxcyExpense.setAuditState("1");
        ISysUser User = new ISysUser();
        User.setRemark("teacher");
        List<ISysUser> list2 = userService.selectMsgUserList(User);
        for (ISysUser user:list2) {
            LysCxcyExpenseWeChatMassage.sendWeChatMassage(cxcyExpense,user);
        }

        return toAjax(cxcyExpenseService.updateCxcyExpense(cxcyExpense));
    }

    /**
     * 删除费用信息
     */
    @RequiresPermissions("project:lysexpense:remove")
    @Log(title = "费用信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cxcyExpenseService.deleteCxcyExpenseByIds(ids));
    }

    /**
     * 审核费用申请
     */
    @RequiresPermissions("project:lysexpense:revise")
    @Log(title = "费用申请", businessType = BusinessType.UPDATE)
    @PutMapping("/shenhe")
    public AjaxResult revise(@RequestBody CxcyExpense cxcyExpense) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyExpense.setAuditState(cxcyExpense.getAuditState());
        Date d = new Date();
        cxcyExpense.setAuditTime(d);
        cxcyExpense.setAuditName(sysUser.getNickName());
        cxcyExpense.setAuditNumber(sysUser.getUserName());
        if (cxcyExpense.getAuditState().equals("2")) {
           LysCxcyExpenseWeChatAuditMassage.sendWeChatAuditMassage(cxcyExpense);

//            CxcyBaseinfo cxcyBaseinfo1 = new CxcyBaseinfo();
//            cxcyBaseinfo1.setStudentNumber(cxcyExpense.getFirstNumber());
//            List<CxcyBaseinfo> list2 = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo1);
//            String kahao = list2.get(0).getCardNumber();//卡号
//            cxcyExpense.setNote1(kahao);
            cxcyExpense.setIsReceive("0");
            cxcyExpenseService.updateCxcyExpense(cxcyExpense);

        }else if(cxcyExpense.getAuditState().equals("3")){
            LysCxcyExpenseWeChatAuditMassage.sendWeChatAuditMassage(cxcyExpense);

        }
        return toAjax(cxcyExpenseService.updateCxcyExpense(cxcyExpense));
    }

    /**
     * 修改收款信息
     */
    @RequiresPermissions("project:lysexpense:modify")
    @Log(title = "收款信息", businessType = BusinessType.UPDATE)
    @PutMapping("/getpay")
    public AjaxResult modify(@RequestBody CxcyExpense cxcyExpense) {
        cxcyExpense.setIsReceive("1");
        CxcyAchievement cxcyAchievement=new CxcyAchievement();
        cxcyAchievement.setId(cxcyExpense.getResultId());
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        list.get(0).setNote10("1");
        cxcyAchievementService.updateCxcyAchievement(list.get(0));
        return toAjax(cxcyExpenseService.updateCxcyExpense(cxcyExpense));
    }




}
